package com.example.howard.xiao7music.presenter;

import android.util.Log;

import com.example.howard.xiao7music.bean.PlayBean;
import com.example.howard.xiao7music.bean.SingerSongBean;
import com.example.howard.xiao7music.bean.SongBySearchBean;
import com.example.howard.xiao7music.bean.SongDownloadBean;
import com.example.howard.xiao7music.bean.SongRankingBean;
import com.example.howard.xiao7music.model.GetRemoteSource;
import com.example.howard.xiao7music.model.GetSearch;
import com.example.howard.xiao7music.model.getbasedata.Injection;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Howard on 2016/9/26.
 */
public class RemoteSongPresenter {
    private GetRemoteSource getRemoteSource=new GetRemoteSource(new GetRemoteSource.callback() {
        @Override
        public void setRankingSongBean(SongRankingBean bean) {

            rankingBean=bean;
            Log.e("presenter",rankingBean.toString());
            rankingSongList=getRankingList();
            Log.e("presenter",rankingSongList.get(1).toString());
            callback.getRankingSongList(rankingSongList);
        }

        @Override
        public void setSingerSongBean(SingerSongBean bean) {

        }

        @Override
        public void setPlaySongURL(String url) {

        }

        @Override
        public void setDownloadSongBean(SongDownloadBean bean) {

        }


    });
    private GetSearch getSearch=new GetSearch(new GetRemoteSource.callback() {
        @Override
        public void setRankingSongBean(SongRankingBean bean) {

        }

        @Override
        public void setSingerSongBean(SingerSongBean bean) {

            searchBySingerSongList=getSearchBySingerSongList(bean);
            callback.getSearchBySinger(searchBySingerSongList);
        }

        @Override
        public void setPlaySongURL(String url) {

        }

        @Override
        public void setDownloadSongBean(SongDownloadBean bean) {

        }
    }, new GetSearch.getSearchcallback() {
        @Override
        public void setSingerSongBeanSearchBySinger() {

        }

        @Override
        public void setSongBySearchBeanListBySong(List<SongBySearchBean> songBySearchBeanList) {

            searchBySongSongList=getSearchBySongSongList(songBySearchBeanList);
            callback.getSearchBySong(searchBySongSongList);
        }

    });
    private SongRankingBean rankingBean;
    private List<Map<String, Object>> rankingSongList=new ArrayList<>();
    private Map<String, Object> map=new HashMap<>();
    private String songname,singername,url,logopath,songid;
    private SingerSongBean singerSongBean;
    private List<SongBySearchBean> songBySearchBeanList;
    private List<Map<String, Object>> searchBySingerSongList=new ArrayList<>();
    private List<Map<String, Object>> searchBySongSongList=new ArrayList<>();


    private callback callback;




    public RemoteSongPresenter(callback callback){
        this.callback=callback;
    }


    public void getRankingSong(int pos){
        int num=1;

        switch (pos){
            case 0:
                num=1;
                break;
            case 1:
                num=2;
                break;
            case 2:
                num=21;
                break;
            case 3:
                num=22;
                break;
            case 4:
                num=23;
                break;
            case 5:
                num=25;
                break;
            default:
                break;
        }
        getRemoteSource.getSongRanking(num);

    }



    private List<Map<String, Object>> getRankingList(){
        for (int i=0;i<rankingBean.getSong_list().size();i++){
            songname=rankingBean.getSong_list().get(i).getTitle();
            singername=rankingBean.getSong_list().get(i).getArtist_name();
            url=null;
            logopath=rankingBean.getSong_list().get(i).getPic_small();
            songid=rankingBean.getSong_list().get(i).getSong_id();
            map=new HashMap<>();
            map.put("SONG",songname);
            map.put("SINGER",singername);
            map.put("URL",url);
            map.put("LOGO",logopath);
            map.put("ID",songid);
            rankingSongList.add(map);
        }

        return rankingSongList;
    }




    public void getSearchBySinger(String query){

        getSearch.getSingerSongSearchbySinger(query);

    }


    public List<Map<String, Object>> getSearchBySingerSongList(SingerSongBean bean){
        List<Map<String, Object>> songList=new ArrayList<>();
        Map<String, Object> map=new HashMap<>();
        String songname,singername,url,logopath,songid;
        if (bean.getSonglist()!=null){
            for (int i=0;i<bean.getSonglist().size();i++){

                songname=bean.getSonglist().get(i).getTitle();
                singername=bean.getSonglist().get(i).getAuthor();
                url=null;
                logopath=bean.getSonglist().get(i).getPic_small();
                songid=bean.getSonglist().get(i).getSong_id();
                map=new HashMap<>();
                map.put("SONG",songname);
                map.put("SINGER",singername);
                map.put("URL",url);
                map.put("LOGO",logopath);
                map.put("ID",songid);
                songList.add(map);
            }
        }


        return songList;

    }

    public void getSearchBySong(String query){

        getSearch.getSongSearchbySong(query);


    }

    public List<Map<String, Object>> getSearchBySongSongList(List<SongBySearchBean> list){

        List<Map<String, Object>> songList=new ArrayList<>();
        Map<String, Object> map=new HashMap<>();
        String songname,singername,url,logopath,songid;

        for (int i=0;i<list.size();i++){
            songname=list.get(i).getSongname();
            singername=list.get(i).getSingername();
            url=null;
            logopath=null;
            songid=list.get(i).getSongid();
            map=new HashMap<>();
            map.put("SONG",songname);
            map.put("SINGER",singername);
            map.put("URL",url);
            map.put("LOGO",logopath);
            map.put("ID",songid);
            songList.add(map);
        }
        return songList;
    }

    public interface callback{
        void getRankingSongList(List<Map<String, Object>> songlist);
        void getSearchBySinger(List<Map<String, Object>> songlist);
        void getSearchBySong(List<Map<String, Object>> songlist);
    }
}
